/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.dataacesslayer;

import mnstudent.datatranferobject.SemesterDTO;
import java.sql.ResultSet;

/**
 *
 * @author trungpv
 */
public class SemesterDAL {
     
    //<editor-fold defaultstate="collapsed" desc="Attributes">
    private SemesterDTO m_SemesterObj;
    private DataExecuteUseStoreProcedure m_DataExecute;
    private String  m_sqlCmd;
    
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="Method">
    public SemesterDAL() {
        this.m_SemesterObj = new SemesterDTO();
        this.m_DataExecute = new DataExecuteUseStoreProcedure();    
        this.m_sqlCmd = "";
    }

    /**
     * Ham lay tat ca cac hoc ky
     *
     * @return boolean
     */
    public ResultSet getAllSemester() {
       try
        {
            m_sqlCmd = "{Call GetAllSemesterFromDatabase}";
            ResultSet _rsData = this.m_DataExecute.getDataFromDatabase(m_sqlCmd,null);
            return _rsData;
        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    /**
     * Ham lay HOCKY theo ID HOCKY
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getSemesterById(String _id)
    {
        try {
            m_sqlCmd = "{Call GetSemesterByIdFromDatabase(?)}";
            String[] arrId = {_id};
            return this.m_DataExecute.getDataFromDatabase(m_sqlCmd, arrId);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return null;
    }
    
     /**
     * ham them moi HOCKY xuong database
     * @param _semester
     * @return true: success, false: fail
     */
    public boolean insertSemesterToDatabase(SemesterDTO _semester)
    {
       try {
           m_sqlCmd = "{Call InsertSemesterToDatabase(?,?)}";
           String[] arrStr = {_semester.getSemesterID(), _semester.getNameOfSemester()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    
    /**
     * Ham cap nhat sua doi thong tin HOCKY
     * @param _semester
     * @return tue: thanh cong, false: that bai
     */
    public boolean updateSemesterToDatabase(SemesterDTO _semester)
    {
       try {
           m_sqlCmd = "{Call UpdateSemesterToDatabase(?,?)}";
           String[] arrStr = {_semester.getSemesterID(), _semester.getNameOfSemester()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    
    /**
     * Ham xoa mot HOCKY ra khoi database
     * @param _semester
     * @return 
     */
     public boolean deleteSemesterFromDatabase(SemesterDTO _semester)
    {
        try {
           m_sqlCmd = "{Call DeleteSemesterFromDatabase(?)}";
           String[] arrStr = {_semester.getSemesterID()};

           return this.m_DataExecute.updateDataToDatabase(m_sqlCmd,arrStr)>0;
           
       } catch (Exception ex) {
          ex.printStackTrace();
       }
        return false;
    }
    //</editor-fold>
}
